/**
 * 
 */
package com.res.simulator.handler;

import com.res.common.handler.CommonHandler;
import com.res.simulator.model.SubmitOrderModel;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.simulator.view.SubmitOrderView;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;

/**
 * @author Dhanushka Ariyasingha.
 * 
 */
public class SubmitOrderHandler implements	CommonHandler<SubmitOrderView, SubmitOrderModel> {

	public SubmitOrderView process(final SubmitOrderModel model)
			throws Exception {

		final CommunicationMessage message = extract(model);

		final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
		final CommunicationMessage returnedMessage = plugin.invoke(message);

		final SubmitOrderView view = new SubmitOrderView();
		final String valueString = returnedMessage.getMessageBody();
		
		System.out.println(valueString);
		
		// Updating view
		update(view, valueString);
		
		// Updating model.
		update(model, valueString);

		return view;
	}
	
	
	private void update(final SubmitOrderView view,final String valueString) throws Exception {

		view.setOfferExternalId(CommonUtils.find(valueString, "OFFER_XID"));
		view.setUserOfferExternalId(CommonUtils.find(valueString, "USER_OFFER_EXTERNAL_ID"));
		view.setUserExternalId(CommonUtils.find(valueString, "USER_ID"));
		view.setProductQuantity(CommonUtils.find(valueString, "PRODUCT_QUANTITY"));

		final String type = CommonUtils.find(valueString, "COS");
		final String channel = CommonUtils.find(valueString, "SIM_CHANNEL");
		view.setType(type.equals("NO") ? channel + " - Purchase " : channel	+ " - COS");
		
	}
	
	private void update(final SubmitOrderModel model, final String valueString) throws Exception{
		
		final String userOfferExternalId = CommonUtils.find(valueString, "USER_OFFER_EXTERNAL_ID");
		final String userExternalId = CommonUtils.find(valueString, "USER_ID");
		
		
		String hiddenValueString = "USER_OFFER_EXTERNAL_ID:" + userOfferExternalId;
		hiddenValueString = hiddenValueString.concat("|USER_ID:"+ userExternalId);
		model.setHiddenValueString(hiddenValueString);
	}

	/**
	 * Value String -
	 *USER_ID:|PAYMENT_METHOD:VOLBILL|SHIPPING:NULL|HMEC:NO|SERVICE_ACTIVATE:NO|COS:NO|TOS:NO|USER_OFFER_EXTERNAL_ID:
	 * 
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	private CommunicationMessage extract(final SubmitOrderModel model) throws Exception {

		
		final CommunicationMessage message = new CommunicationMessage();

		String valueString = model.getValueString();
		valueString = valueString.concat("|SIM_CHANNEL:" + model.getChannel());
		valueString = valueString.concat("|OFFER_XID:" +  model.getOfferXID().trim());
		valueString = valueString.concat("|PRODUCT_QUANTITY:" +  model.getProductQuanity().trim());

		final String hiddenValues = model.getHiddenValueString();
		String userOfferExternalIdHidden = null;
		String userId = null;
		
		if(hiddenValues.contains("USER_OFFER_EXTERNAL_ID")){
			userOfferExternalIdHidden = CommonUtils.find(hiddenValues, "USER_OFFER_EXTERNAL_ID");
		}

		if(hiddenValues.contains("USER_ID")){
			userId = CommonUtils.find(hiddenValues, "USER_ID");
		}

		
		if(valueString.contains("COS:YES") && null != userOfferExternalIdHidden && ! userOfferExternalIdHidden.contains("NULL")){
			
			final String userOfferExternalPassed = CommonUtils.find(valueString, "USER_OFFER_EXTERNAL_ID");
			
			if(userOfferExternalPassed.equals("NULL")){
				
				System.out.println(hiddenValues);

				valueString = valueString.replaceAll("USER_OFFER_EXTERNAL_ID:", "USER_OFFER_EXTERNAL_ID:" +  userOfferExternalIdHidden);
				valueString = valueString.replaceAll("USER_ID:", "USER_ID:" +  userId);
			}
			
		}
		
		message.setMessageHeader("JOBType:SubmitOrder|ID:dev44.aepona.com");
		message.setMessageBody(valueString);
		
		return message;
	}

}
